<template>
  <header class="todo-header">
    <h1>TodoList</h1>
    <div class="add-todo">
      <input
        type="text"
        placeholder="请输入待办事项..."
        v-model.trim="content"
        @keyup.enter="addTodo"
      />
      <button @click="addTodo">添加</button>
    </div>
  </header>
</template>

<script setup>
import { ref } from 'vue';

const content = ref('');

import { v4 as uuidv4 } from 'uuid';      //这个模块可以创建唯一标识符id

const emit = defineEmits(['add-todo']);

const addTodo = () => {
    if (content.value) {
        const todo = {
            id: uuidv4(),
            content: content.value,
            completed: false,
        };
        emit('add-todo', todo);
        content.value = '';
    }
}
</script>

<style scoped>
.todo-header {
  padding: 5px 0 10px;
  background-color: #0b87ff;
  color: #f0f8ff;
  text-align: center;
}
.todo-header h1 {
  font-size: 24px;
}
.todo-header .add-todo {
  margin-top: 10px;
  display: flex;
  gap: 5px;
  justify-content: center;
}
.todo-header .add-todo input {
  width: 300px;
  height: 30px;
  padding: 0 10px;
  border-radius: 3px;
  outline: none;
  border: solid 1px white;
}
.todo-header .add-todo button {
  display: inline-block;
  width: 80px;
  background-color: #eee;
  color: #0b87ff;
  line-height: 30px;
  border: none;
  border-radius: 3px;
}
.todo-header .add-todo button:hover {
  cursor: pointer;
  background-color: #fff;
  color: #ff6700;
}
</style>
